package com.olympus.hermes.common.constants;

/**
 * 枚举超类，所有枚举都要实现BaseEnum
 *
 * @author Mickey
 * @since 2019.11.24
 */
public interface BaseEnum<T extends Enum<T> & BaseEnum<T>> {

    /**
     * get code
     * @return code
     */
    Integer getCode();

    /**
     * get name
     * @return name
     */
    String getName();

    /**
     * use code find enum
     * @param cls class
     * @param code code
     * @param <T> type
     * @return enum
     */
    static<T extends Enum<T> & BaseEnum<T>> T parseByCode(Class<T> cls, Integer code){
        for (T t : cls.getEnumConstants()){
            if (t.getCode().intValue() == code.intValue()){
                return t;
            }
        }
        return null;
    }

}
